//
//  SXWelcomeViewController.swift
//  sina
//
//  Created by shengxinwei on 16/5/23.
//  Copyright © 2016年 shengxinwei. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class SXWelcomeViewController: UIViewController {
    
    override func loadView() {
        //切换视图
        view = backView
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(iconView)
        view.addSubview(nameLabel)
        
        iconView.snp_makeConstraints { (make) in
            make.centerX.equalTo(self.backView)
            make.bottom.equalTo(self.backView.snp_bottom).offset(-160)
            make.height.width.equalTo(90)
        }
        nameLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(self.backView)
            make.top.equalTo(self.iconView.snp_bottom).offset(16)
        }
        nameLabel.alpha = 0;
        //设置头像图像圆角
        iconView.cornerRadius = 45

        //加载头像
        iconView.sd_setImageWithURL(SXUserAccountViewModel.sharedAccount.headUrl)
        
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(true)
        
        let offsetY = -(view.bounds.size.height) + 160
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0.5, options: [], animations: {
            //更新约束
            self.iconView.snp_updateConstraints(closure: { (make) in
                make.bottom.equalTo(self.view).offset(offsetY)
            })
            
            //提前刷新父视图
            self.view.layoutIfNeeded()
        }) { (_) in
            
            UIView.animateWithDuration(0.5, animations: {
                 //设置字体隐藏显示
                self.nameLabel.alpha = 1;
                }, completion: { (_) in
                    //发送通知跳转到首页
                    NSNotificationCenter.defaultCenter().postNotificationName("SwitchRootViewController", object: "main")
            })
        }
    }
    
    override func prefersStatusBarHidden() -> Bool {
        return true
    }
    
    
    //MARK: - 懒加载
    
    //背景图片
    lazy var backView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    //头像
    lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    
    //微博名
    lazy var nameLabel: UILabel = {
       var lab = UILabel()
        lab.text = SXUserAccountViewModel.sharedAccount.userAccount?.name
        lab.textAlignment = .Center
        lab.textColor = UIColor.blackColor()
        return lab
    }()
    
}
